package com.example.stroebackstage.servlet;
import com.example.stroebackstage.entity.Commodity;
import com.example.stroebackstage.service.CommodityService;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
//import org.apache.commons.fileupload.disk.DiskFileItem.*;

public class CommodityServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String toDo = req.getServletPath();
        HashMap<Integer,String> categoryMap = (HashMap<Integer, String>) req.getSession().getAttribute("categoryMap");
//        查找分类
        if ( categoryMap == null){
            categoryMap = new CommodityService().selectCategory();
        }
//        删除分类
        if (toDo.equals("/toDelCategory")){
            String sid = req.getParameter("id");
            int id = Integer.parseInt(sid);
            boolean check = new CommodityService().delCategory(id);
            if (check){
                categoryMap = new CommodityService().selectCategory();
            }
        }
//        修改分类
        if (toDo.equals("/toUpdateCategory")){
            String sid = req.getParameter("id");
            int id = Integer.parseInt(sid);
            String name = req.getParameter("name");
            boolean check = new CommodityService().updateCommodityCategory(id,name);
            if (check){
                categoryMap = new CommodityService().selectCategory();
            }
        }
//        添加分类
        if (toDo.equals("/toInsertCategory")){
            String name = req.getParameter("name");
            boolean check = new CommodityService().insertCategory(name);
            if (check){
                categoryMap = new CommodityService().selectCategory();
            }
        }

//        展示全部商品
        if (toDo.equals("/toShowAllCommodity")){
            ArrayList<Commodity> commodityList = new CommodityService().selectAll();
            req.getSession().setAttribute("commodityList", commodityList);
        }
//        查询商品
        if (toDo.equals("/toFindCommodity")){
            String BySth = req.getParameter("by");
            String massage = req.getParameter("search");
            if("ID".equals(BySth)){
                int id = Integer.parseInt(massage);
                ArrayList<Commodity> commodityList = new CommodityService().selectById(id);
                req.getSession().setAttribute("commodityList", commodityList);
            }
            if("name".equals(BySth)){
                ArrayList<Commodity> commodityList = new CommodityService().selectByName(massage);
                req.getSession().setAttribute("commodityList", commodityList);
            }
        }
//        删除商品
        if (toDo.equals("/toDelCommodity")){
            String sid = req.getParameter("id");
            int id = Integer.parseInt(sid);
            boolean check = new CommodityService().del(id);
            if (check){
                ArrayList<Commodity> commodityList = new CommodityService().selectAll();
                req.getSession().setAttribute("commodityList", commodityList);
            }
        }
//        修改商品
        if (toDo.equals("/toUpdateCommodity")){
            String sid,scategory,name,img, model,introduction,sprice;
            sid = req.getParameter("id");
            scategory = req.getParameter("category");
            name = req.getParameter("name");
            img = req.getParameter("img");
            model = req.getParameter("model");
            introduction = req.getParameter("introduction");
            sprice = req.getParameter("price");
            int id = Integer.parseInt(sid);
            int category = Integer.parseInt(scategory);
            double price = Double.parseDouble(sprice);
            Commodity commodity = new Commodity(id,category,name,img, model,introduction,price);
            boolean check = new CommodityService().updateCommodity(commodity);
            if (check){
                ArrayList<Commodity> commodityList = new CommodityService().selectAll();
                req.getSession().setAttribute("commodityList", commodityList);
            }
            
        }
//        添加商品
        if (toDo.equals("/toInsertCommodity")){
            String scategory,name,img, model,introduction,sprice;
            scategory = req.getParameter("category");
            name = req.getParameter("name");
            img = req.getParameter("img");
            model = req.getParameter("model");
            introduction = req.getParameter("introduction");
            sprice = req.getParameter("price");

            int category = Integer.parseInt(scategory);
            double price = Double.parseDouble(sprice);
            Commodity commodity = new Commodity(category,name,img, model,introduction,price);
            boolean check = new CommodityService().insert(commodity);
            if (check){
                ArrayList<Commodity> commodityList = new CommodityService().selectAll();
                req.getSession().setAttribute("commodityList", commodityList);
            }
            
        }
        req.getSession().setAttribute("categoryMap", categoryMap);
//        重定向

        resp.sendRedirect(req.getContextPath()+"/commodity.jsp");
    }
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }
}
